
/*实现级联勾选开始*/
var nodeCheckedSilent = false;
function nodeChecked (event, node){
    if(nodeCheckedSilent){
        return;
    }
    nodeCheckedSilent = true;
    checkAllParent(node);
    checkAllSon(node);
    nodeCheckedSilent = false;
}

var nodeUncheckedSilent = false;
function nodeUnchecked  (event, node){
    if(nodeUncheckedSilent)
        return;
    nodeUncheckedSilent = true;
    uncheckAllParent(node);
    uncheckAllSon(node);
    nodeUncheckedSilent = false;
}

//选中全部父节点
function checkAllParent(node){
    $('#tree').treeview('checkNode',node.nodeId,{silent:true});
    var parentNode = $('#tree').treeview('getParent',node.nodeId);
    if(!("nodeId" in parentNode)){
        return;
    }else{
        checkAllParent(parentNode);
    }
}
//取消全部父节点
function uncheckAllParent(node){
    $('#tree').treeview('uncheckNode',node.nodeId,{silent:true});
    var siblings = $('#tree').treeview('getSiblings', node.nodeId);
    var parentNode = $('#tree').treeview('getParent',node.nodeId);
    if(!("nodeId" in parentNode)) {
        return;
    }
    var isAllUnchecked = true;  //是否全部没选中
    for(var i in siblings){
        if(siblings[i].state.checked){
            isAllUnchecked=false;
            break;
        }
    }
    if(isAllUnchecked){
        uncheckAllParent(parentNode);
    }

}

//级联选中所有子节点
function checkAllSon(node){
    $('#tree').treeview('checkNode',node.nodeId,{silent:true});
    if(node.nodes!=null&&node.nodes.length>0){
        for(var i in node.nodes){
            checkAllSon(node.nodes[i]);
        }
    }
}
//级联取消所有子节点
function uncheckAllSon(node){
    $('#tree').treeview('uncheckNode',node.nodeId,{silent:true});
    if(node.nodes!=null&&node.nodes.length>0){
        for(var i in node.nodes){
            uncheckAllSon(node.nodes[i]);
        }
    }
}
/*实现级联勾选结束*/









function fmtState(value){
    return value==1?"启用":"禁用";
}

function fmtOpt(value,row,index) {
    var edit = `<a class="btn btn-primary edit" href="javascript:void(0)"><i class="fa fa-edit"></i> 修改</a>`;
    var del= `<a class="btn btn-danger del" href="javascript:void(0)"><i class="fa fa-remove"></i> 删除</a>`;
    return edit+"&emsp;"+del;
}
$(function(){

    $("#update").dialog({
        title:"修改角色",
        btnName:"修改",
        btnBind:function(){
            var params=$("#update form").serialize();
            $.post("sys/role/update",params,function(msg){
                $("#update").dialog("close");
                $("#roleTb").bootstrapTable("refresh");
                toastr.warning(msg);
            },'text');
        }
    });
});
var eventObj={
    "click .edit":function(event,value,row,index){
        $("#update form").setForm(row);
        $("#update").dialog("open");
    },
    "click .del":function(event,value,row){
        swal({
            title:"确实要删除么？",
            text:"该操作不可逆，请谨慎操作！",
            type:"warning",
            confirmButtonText:"确认",
            showCancelButton:true,
            cancelButtonText:"取消"
        }).then(function(){
            $.get("sys/role/del","id="+row.id,function(msg){
                $("#roleTb").bootstrapTable("refresh");
                toastr.warning(msg);
            },'text');
        });
    }
}
$(function(){
$("#grant").dialog({
    title:"角色授权",
    btnName:"授权确认",
    btnBind:function(){
        var arr1=$("#roleTb").bootstrapTable("getSelections");
        var arr2=$("#tree").treeview("getChecked");
        var params="rid="+arr1[0].id;
        $(arr2).each(function(){
            params+="&mids="+this.id;
        });
        $.post("sys/permission/grant",params,function(msg){
            toastr.warning(msg);
            $("#grant").dialog("close");
        },'text');
    }
});

});
function grant(){
    var rows=$("#roleTb").bootstrapTable("getSelections");
    if(rows.length!=1){
        swal({
            title:"信息提示",
            text:"必须选择一个角色待授权！",
            type:"warning",
            confirmButtonText:"确认"
        });
        return;
    }

    $.getJSON("sys/permission/showTree?rid="+rows[0].id,function(data){
        $("#tree").treeview({
            data:data,
            showCheckbox:true,
            multiSelect:true,
            onNodeChecked:nodeChecked,
            onNodeUnchecked:nodeUnchecked
        });
        $("#grant").dialog("open");
    });

}


